package work;

import com.google.common.base.Joiner;
import work.fram.JDBCConnect;
import work.fram.WriterMaker;
import work.getter.Dict;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;

public class ValueToCode {

    private static String tableName = "tc_md_personnel";

    private static String idName = "sysid";

    private static Map<String, Map<String, String>> mapMap = new HashMap<>();

    static {
        mapMap.put("degree", Dict.getByType("学历字典"));
        mapMap.put("jobs", Dict.getByType("岗位字典"));
        mapMap.put("person_category", Dict.getByType("人员类别字典"));
        mapMap.put("post_type", Dict.getByType("任职类型"));
        mapMap.put("technical_position", Dict.getByType("专业技术职务字典"));
        mapMap.put("devote_major", Dict.getByType("从事专业字典"));
        mapMap.put("person_sort", Dict.getByType("人员分类"));
        mapMap.put("entry_sources", Dict.getByType("进入来源字典"));
        mapMap.put("household_nature", Dict.getByType("户口性质字典"));
        mapMap.put("politic_face", Dict.getByType("政治面貌字典"));
        mapMap.put("marital_status", Dict.getByType("婚姻状况字典"));
        mapMap.put("health_status", Dict.getByType("健康状况字典"));

     /*   Map<String, String> re = new HashMap<>();

        Map<String, String> map = Dict.getByType("政治面貌字典");

        map.forEach((k, v) -> {
            re.put(v, k);
        });
*/
     //   mapMap.put("political", mapMap);

    }


    public static void main(String[] args) {


        WriterMaker.MyWriter writer = WriterMaker.create("将字典值更新为code.sql");

        List<String> sqls = new ArrayList<>();

        //首先处理所有元素

        Statement statement = JDBCConnect.getStatement();

        try {
            ResultSet resultSet = statement.executeQuery("SELECT * FROM " + tableName +" where create_time=\"2019-11-22 0:00:00\"");//

            Set<String> names = mapMap.keySet();

            while (resultSet.next()) {

                List<String> to = new ArrayList<>();
                for (String name : names) {
                    String v = null;
                    try {
                        v = resultSet.getString(name);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    if (v != null) {
                        String mapValue = map(name, v);
                        if (mapValue != null && !v.equals(mapValue)) {
                            to.add(name + "=\"" + mapValue + "\"");
                        }
                    }
                }
                if (to.size() > 0) {
                    String sql = "UPDATE " + tableName + " SET " + Joiner.on(",").join(to) + " where " + idName + "=\"" + resultSet.getString(idName) + "\";";
                    System.out.println(sql);
                    sqls.add(sql);
                }
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }


        for (String sql : sqls) {
            writer.writeLine(sql);
        }

        writer.close();

    }



    private static String map(String col, String value) {

        Map<String, String> map = mapMap.get(col);
        if (map == null) {
            return null;
        }

        return map.get(value.trim());
    }

}
